var contact = function () {
    var contactInfo = this;
    this.URL = {
        list: ctx+"/user/addressList",
        excel: ctx+"/user/addressExcel"
    }
    //初始化查询
    this.initQuery = function () {
        /**
         * 初始化页面数据
         * **/

        //初始化表格
        this.initGrid = function () {
            $('#dataList').datagrid({
                singleSelect: false,
                collapsible: true,
                pagination: true,
                method: 'post',
                remoteSort: false,
                checkOnSelect: true,
                rownumbers: true,
                toolbar: '#tb',
                fit: true,
                pageSize: 10,
                pageList: [10, 50, 100],
                columns: [
                    [{field: 'ck', checkbox: true},
                        {field: 'id', hidden: true},
                        {field: 'userName', title: '用户名', width: 200, align: 'center', sortable: false},
                        {field: 'address', title: '地址', width: 400, align: 'center', sortable: false},
                        {
                            field: 'isDefault',
                            title: '是否默认',
                            width: 200,
                            align: 'center',
                            sortable: true,
                            styler: function (value, row, index) {
                                if (value == 0) {
                                    return 'color:red;';
                                }
                                if (value == 1) {
                                    return 'color:#000088;';
                                }
                            },
                            formatter: function (value) {
                                if (value == "1") return "是";
                                if (value == "0") return "否";
                            }
                        },
                        {
                            field: 'recStatus',
                            title: '状态',
                            width: 200,
                            align: 'center',
                            sortable: false,
                            styler: function (value, row, index) {
                                if (value == 0) {
                                    return 'color:red;';
                                }
                                if (value == 1) {
                                    return 'color:#000088;';
                                }
                            },
                            formatter: function (value) {
                                if (value == "1") return "启用";
                                if (value == "0") return "停用";
                            }
                        }
                    ]]
            });
        }

    }

    //初始化按钮功能
    this.initFun = function () {

        //查询按钮
        $("#searchBtn").click(function () {

            var options = $('#dataList').datagrid('options');
            options.url = contactInfo.URL.list;
            options.queryParams = {
                telphone: $('#telphone').val(),
                userName: $('#userName').val()
            };
            $('#dataList').datagrid("load"); //不要使用$('#dataList').datagrid(options);防止二次渲染
        });

//导出全部数据按钮
        $("#exportAllBtn").click(function () {
            //创建一个表单提交
            $("#excelForm").remove();
            var excelForm = $("<form id='excelForm' method='post' action='" + contactInfo.URL.excel + "'></form>");
            var options = $('#dataList').datagrid('options');
            var rows = $('#dataList').datagrid('getRows');
            var params = options.queryParams;
            if (!params || $.isEmptyObject(params) || rows.length < 1) {
                $.messager.alert('提示', '没有数据可以导出，请先查询！');
                return;
            }
            $.messager.confirm('提示', '您确定导出当前的的数据吗?', function (r) {
                if (r) {
                    params['sort'] = options.sortName;
                    params['order'] = options.sortOrder;
                    for (var p in params) {//遍历json对象的每个key/value对,p为key
                        excelForm.append("<input type='hidden' name='" + p + "' value='" + params[p] + "' />");
                    }
                    $("body").append(excelForm);
                    excelForm[0].submit();
                }
            })

        });


    }

    this.init = function () {
        contactInfo.initQuery();
        contactInfo.initGrid()
        contactInfo.initFun();
    }

};

$(function () {
    new contact().init();
});




